package com.teamcenter.rac.massupdate.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.core.databinding.AggregateValidationStatus;
import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.ValidationStatusProvider;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.wb.swt.SWTResourceManager;

import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.massupdate.Activator;
import com.teamcenter.rac.massupdate.model.UpdateJobModel;
import com.teamcenter.rac.viewer.sorter.PropertyColumnViewerSorter;
@SuppressWarnings({"rawtypes","unchecked"})
public abstract class AbstractBindingWizardPage extends WizardPage {
	public final WritableValue m_observeValidationState = new WritableValue(Boolean.FALSE, Boolean.class);
	public final WritableValue m_observeValidationText = new WritableValue("", IStatus.class);
	List<ValidationStatusProvider> m_validationStatusProviders;
	protected DataBindingContext m_bindingContext;
	private int m_debug_ListFailedValidationLevel = 1;
	private Color m_invalidControlBackgroundColor = SWTResourceManager.getColor(240, 200, 200);
	private Logger m_logger;
	private final IObservableValue m_observeDialogMessageText = new IC_ObserverForDialogMessageText();
	private IC_ObserverForDialogButtonUpdates m_observerDialogButtonUpdates = new IC_ObserverForDialogButtonUpdates();
	private TCSession m_tcSession;

	protected AbstractBindingWizardPage(String paramString, UpdateJobModel paramUpdateJobModel) {
		super(paramString);
	}

	public boolean canExecute() {
		return false;
	}

	public boolean canFlipToNextPage() {
		Object localObject = this.m_observeValidationState.getValue();
		if ((localObject instanceof Boolean)) {
			return ((Boolean) localObject).booleanValue();
		}
		return false;
	}

	public boolean canManageMarkup() {
		return false;
	}

	public boolean canSaveAsMarkup() {
		return false;
	}

	public void createControl(Composite paramComposite) {
		Composite localComposite = new Composite(paramComposite, 0);
		setControl(localComposite);
	}

	protected Logger getLogger() {
		if (this.m_logger == null) {
			this.m_logger = Logger.getLogger(AbstractBindingWizardPage.class);
		}
		return this.m_logger;
	}

	protected TCSession getSession() {
		try {
			if (this.m_tcSession == null) {
				this.m_tcSession = ((TCSession) Activator.getDefault().getSessionService()
						.getSession(TCSession.class.getName()));
			}
		} catch (Exception localException) {
			getLogger().error(localException.getLocalizedMessage(), localException);
		}
		return this.m_tcSession;
	}

	protected void initCommonDataBindings(DataBindingContext paramDataBindingContext) {
		paramDataBindingContext.bindValue(this.m_observerDialogButtonUpdates, this.m_observeValidationState, null,
				null);
		paramDataBindingContext.bindValue(this.m_observeDialogMessageText, this.m_observeValidationText, null, null);
	}

	protected DataBindingContext initDataBindings() {
		return new DataBindingContext();
	}

	protected void setupAggregateValidationStatus() {
		IObservableList localIObservableList = this.m_bindingContext.getValidationStatusProviders();
		this.m_validationStatusProviders = new ArrayList(localIObservableList.size());
		Iterator<ValidationStatusProvider> localIterator = localIObservableList.iterator();
		while (localIterator.hasNext()) {
			ValidationStatusProvider localObject = localIterator.next();
			this.m_validationStatusProviders.add((ValidationStatusProvider) localObject);
		}
		Object localObject = new AggregateValidationStatus(localIObservableList, 2);
		((AggregateValidationStatus) localObject).addValueChangeListener(new IValueChangeListener() {
			public void handleValueChange(ValueChangeEvent paramAnonymousValueChangeEvent) {
				IStatus localIStatus = (IStatus) paramAnonymousValueChangeEvent.diff.getNewValue();
				if (localIStatus.getSeverity() != 0) {
					logAllValidationStatus();
					AbstractBindingWizardPage.this.m_observeValidationText.setValue(localIStatus);
					AbstractBindingWizardPage.this.m_observeValidationState.setValue(Boolean.FALSE);
				} else {
					AbstractBindingWizardPage.this.m_observeValidationText.setValue(null);
					AbstractBindingWizardPage.this.m_observeValidationState.setValue(Boolean.TRUE);
				}
				updateControlBackgrounds();
			}

			private void logAllValidationStatus() {
				if (AbstractBindingWizardPage.this.m_debug_ListFailedValidationLevel > 0) {
					Iterator localIterator = AbstractBindingWizardPage.this.m_bindingContext
							.getValidationStatusProviders().iterator();
					while (localIterator.hasNext()) {
						Object localObject = localIterator.next();
						IObservableValue localIObservableValue = ((ValidationStatusProvider) localObject)
								.getValidationStatus();
						Status localStatus = (Status) localIObservableValue.getValue();
						if ((localStatus.getSeverity() != 0)
								&& (AbstractBindingWizardPage.this.m_debug_ListFailedValidationLevel > 1)
								&& (localStatus.getSeverity() != 2)) {
							AbstractBindingWizardPage.this.getLogger().info(localStatus);
						}
					}
				}
			}

			private void updateControlBackgrounds() {
				Iterator localIterator = AbstractBindingWizardPage.this.m_bindingContext.getValidationStatusProviders()
						.iterator();
				while (localIterator.hasNext()) {
					Object localObject1 = localIterator.next();
					IObservableValue localIObservableValue = ((ValidationStatusProvider) localObject1)
							.getValidationStatus();
					Status localStatus = (Status) localIObservableValue.getValue();
					if ((localObject1 instanceof Binding)) {
						IObservable localIObservable = ((Binding) localObject1).getTarget();
						Control localControl = null;
						Object localObject2;
						if ((localIObservable instanceof Control)) {
							localControl = (Control) localIObservable;
						} else if ((localIObservable instanceof ISWTObservableValue)) {
							localObject2 = ((ISWTObservableValue) localIObservable).getWidget();
							if ((localObject2 instanceof Control)) {
								localControl = (Control) localObject2;
							}
						}
						if ((localControl != null) && (!localControl.isDisposed())) {
							localObject2 = null;
							if (localStatus.getSeverity() != 0) {
								localObject2 = AbstractBindingWizardPage.this.m_invalidControlBackgroundColor;
							}
							localControl.setBackground((Color) localObject2);
						}
					}
				}
			}
		});
	}

	private class IC_ObserverForDialogButtonUpdates extends WritableValue {
		private IC_ObserverForDialogButtonUpdates() {
		}

		public void doSetValue(Object paramObject) {
			super.doSetValue(paramObject);
			IWizardContainer localIWizardContainer = AbstractBindingWizardPage.this.getContainer();
			if (localIWizardContainer != null) {
				boolean debug=true;
				if(debug){
					localIWizardContainer.updateButtons();
				}
				else{
					
				}
			}
		}
	}

	private class IC_ObserverForDialogMessageText extends WritableValue {
		public IC_ObserverForDialogMessageText() {
			super(IStatus.class, null);
		}

		public void doSetValue(Object paramObject) {
			if ((paramObject instanceof IStatus)) {
				IStatus localIStatus = (IStatus) paramObject;
				switch (localIStatus.getSeverity()) {
				case 1:
					AbstractBindingWizardPage.this.setMessage(localIStatus.getMessage(), 1);
					break;
				case 2:
					AbstractBindingWizardPage.this.setMessage(localIStatus.getMessage(), 2);
					break;
				case 4:
					AbstractBindingWizardPage.this.setMessage(localIStatus.getMessage(), 3);
					break;
				case 3:
				default:
					AbstractBindingWizardPage.this.setMessage(localIStatus.getMessage(), 0);
					break;
				}
			} else {
				AbstractBindingWizardPage.this.setMessage("", 0);
			}
		}
	}

	protected static class IC_PropertyColumnViewerSorter extends PropertyColumnViewerSorter {
		protected Object getValue(Object paramObject, String paramString, int paramInt, Viewer paramViewer) {
			if ((paramObject instanceof IPropertySource)) {
				return ((IPropertySource) paramObject).getPropertyValue(paramString);
			}
			return super.getValue(paramObject, paramString, paramInt, paramViewer);
		}
	}
}
